
function Main
{
    Param(
        [parameter(Mandatory=$true)]
        [string]$new_file_dir,
        [parameter(Mandatory=$true)]
        [string[]]$iis_webSite,
        [parameter(Mandatory=$true)]
        [string]$isBackUpWebSite
    )

    Process
    {
        try{
            Get-WriteLog('')
            Get-WriteLogFormat('Update Project Start')
            Get-WriteLog('')

            Get-WriteLogFormat('Setting Info Start')
            Get-WriteLog('Update dir: ' + $new_file_dir)
            Get-WriteLog('Update Web Site: ' + $iis_webSite)
            Get-WriteLog('Backup file: ' + $isBackUpWebSite)
            Get-WriteLogFormat('Setting Info Stop')
            Get-WriteLog('')

            # 0.檢查更新文件
            $ext = Test-Path $new_file_dir
            if($ext -eq $True)
            { 
                $file = Get-AllFile $new_file_dir 
                if($file.count -gt 0)
                {
                    Get-WriteLogFormat('Check Update Project File - Running')
                    Get-WriteLog('update files: ')
                    Get-WriteLogArr($file)
                    Get-WriteLogFormat('Check Update Project File - End')
                    Get-WriteLog('')
                }
                else{
                    Get-WriteLogFormat('Check Update Project File - Running')
                    Get-WriteLog('not file need to update')
                    Get-WriteLogFormat('Check Update Project File - End')
                    Get-WriteLog('')
                    Get-WriteLogFormat('Update Project End')
                    return
                }
            }
        
            Get-WriteLogFormat('Stop Web Site - Running')
            # 2.iis 網站暫停
            foreach($site in $iis_webSite)
            {
                $status = Get-WebSiteState -Name $site
                Get-WriteLog('Web Site: ' + $site + ',State: ' + $status.Value)
                if($status.Value -eq "Started")
                {
                    Get-WriteLog('Stop Web Site: ' + $site + ' Start')
                    Stop-WebSite -Name $site
                    Get-WriteLog('Stop Web Site: ' + $site + ' End')

                    $status = Get-WebSiteState -Name $site
                    Get-WriteLog('Web Site: ' + $site + ',State: ' + $status.Value)
                }
                Get-WriteLog('-------------')
            }
            Get-WriteLogFormat('Stop Web Site - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Stop App Pool - Running')
            # 1.iis 程序池暫停
            foreach($site in $iis_webSite)
            {
                $site_info = Get-WebSite -Name $site
                if($site_info -ne $null)
                {
                    $pool = $site_info.applicationPool
                    if(($pool -ne $null) -and ($pool -ne ''))
                    {
                        $status = Get-WebAppPoolState -Name $pool
                        Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                        if($status.Value -eq "Started")
                        {
                            Get-WriteLog('Stop App Pool: ' + $pool + ' Start')
                            Stop-WebAppPool -Name $pool
                            Get-WriteLog('Stop App Pool: ' + $pool + ' End')

                            $status = Get-WebAppPoolState -Name $pool
                            Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)

                            if($status.Value -eq "Stopping")
                            {
                                # Stopping 等待轉成 Stopped
                                do{
                                    # 休眠1秒
                                    Start-Sleep -Seconds 1
                                    Get-WriteLog('Sleep: 1')
                                } while($status.Value -eq "Stopping")

                                $status = Get-WebAppPoolState -Name $pool
                                Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                            }
                        } elseif($status.Value -eq "Stopping")
                        {
                            # Stopping 等待轉成 Stopped
                            do{
                                # 休眠1秒
                                Start-Sleep -Seconds 1
                                Get-WriteLog('Sleep: 1')
                            } while($status.Value -eq "Stopping")

                            $status = Get-WebAppPoolState -Name $pool
                            Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                        }
                        Get-WriteLog('-------------')
                    }
                }
            }
            Get-WriteLogFormat('Stop App Pool - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Backup Project File - Running')
            # 3.備份項目文件
            if($isBackUpWebSite -eq 'true')
            {
                foreach($site in $iis_webSite)
                {
                    $site_info = Get-WebSite -Name $site
                    if($site_info -ne $null)
                    {
                        $file = $site_info.physicalPath
                        if(($file -ne $null) -and ($file -ne ''))
                        {
                            $newfile = $file + '_Copy'
                            $ext = Test-Path $newfile
                            if($ext -eq $false)
                            {
                                Get-WriteLog('create directory: ' + $newfile + ' start')
                                #創建文件夾
                                $nitem = New-Item -Path $newfile -type "directory"
                                Get-WriteLog('create directory: ' + $newfile  + ' end')
                            }
                            Get-WriteLog('backup file: ' + $file + ' to ' + $newfile + ' start')
                            #拷貝文件
                            $citem = Copy-Item -Path $file\* -Destination $newfile\ -Recurse -Force
                            Get-WriteLog('backup file: ' + $file + ' to ' + $newfile + ' end')
                            Get-WriteLog('-------------')
                        }
                    }
                }
            }else{
                Get-WriteLog('Do not need Backup Project File')  
            }
            Get-WriteLogFormat('Backup Project File - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Update Project File - Running')
            # 4.更新項目
            $ext = Test-Path $new_file_dir
            if($ext -eq $True)
            {
                # Get-WriteLog('update files: ')
                # Get-AllFile $new_file_dir 
                # Get-WriteLog('-------------')

                foreach($site in $iis_webSite)
                {
                    $site_info = Get-WebSite -Name $site
                    if($site_info -ne $null)
                    {
                        $file = $site_info.physicalPath
                        if(($file -ne $null) -and ($file -ne ''))
                        {
                            $file_ext = Test-Path $file
                            if($file_ext -eq $True)
                            {
                                Get-WriteLog('copy files: ' + $new_file_dir + ' to ' + $file + ' start')
                                # -Recurse 複製文件夾下子文件夾中的文件， -Force 強制複製
                                $citem = Copy-Item -Path $new_file_dir\* -Destination $file\ -Recurse -Force
                                Get-WriteLog('copy files: ' + $new_file_dir + ' to ' + $file + ' end')
                                Get-WriteLog('-------------')
                            }
                        }
                    }
                }
            }
            Get-WriteLogFormat('Update Project File - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Start App Pool - Running')
            # 5.iis 程序池啟動
            foreach($site in $iis_webSite)
            {
                $site_info = Get-WebSite -Name $site
                if($site_info -ne $null)
                {
                    $pool = $site_info.applicationPool
                    if(($pool -ne $null) -and ($pool -ne ''))
                    {
                        $status = Get-WebAppPoolState -Name $pool
                        Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                        if($status.Value -eq "Stopped")
                        {
                            Get-WriteLog('Start App Pool: ' + $pool + ' Start')
                            Start-WebAppPool -Name $pool
                            Get-WriteLog('Start App Pool: ' + $pool + ' End')

                            $status = Get-WebAppPoolState -Name $pool
                            Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                        }elseif($status.Value -eq "Stopping")
                        {
                            # Stopping 等待轉成 Stopped 
                            do{
                                # 休眠1秒
                                Start-Sleep -Seconds 1
                                Get-WriteLog('Sleep: 1')
                            } while($status.Value -eq "Stopping")

                            $status = Get-WebAppPoolState -Name $pool
                            Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)

                            if($status.Value -eq "Stopped")
                            {
                                Get-WriteLog('Start App Pool: ' + $pool + ' Start')
                                Start-WebAppPool -Name $pool
                                Get-WriteLog('Start App Pool: ' + $pool + ' End')

                                $status = Get-WebAppPoolState -Name $pool
                                Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                            }
                        }
                        Get-WriteLog('-------------')
                    }
                }
            }
            Get-WriteLogFormat('Start App Pool - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Start Web Site - Running')
            # 6.iis 網站啟動
            foreach($site in $iis_webSite)
            {
                $status = Get-WebSiteState -Name $site
                Get-WriteLog('Web Site: ' + $site + ',State: ' + $status.Value)
                if($status.Value -eq "Stopped")
                {
                    Get-WriteLog('Start Web Site: ' + $site + ' Start')
                    Start-WebSite -Name $site
                    Get-WriteLog('Start Web Site: ' + $site + ' End')

                    $status = Get-WebAppPoolState -Name $pool
                    Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                }
                Get-WriteLog('-------------')
            }
            Get-WriteLogFormat('Start Web Site - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Visit Web Site - Running')
            # 引入腳本
            # $script = ".\get-iiswebsiteurl.ps1"
            # . ($script)

            #iis 訪問網站
            foreach($site in $iis_webSite)
            {
                [string[]] $urls = Get-IISWebSiteUrl -name $site
                Get-WriteLog('all url: ')
                Get-WriteLogArr($urls)
                Get-WriteLog('-------------')
                if($urls.count -gt 0)
                {
                    Get-WriteLog('visit url: ' + $urls[0])    
                    #訪問綁定的第一個網址   
                    Start-Process -FilePath $urls[0]   
                }
                Get-WriteLog('-------------')
            }
            Get-WriteLogFormat('Visit Web Site - End')

            Get-WriteLog('')
            Get-WriteLogFormat('Update Project End')
            Get-WriteLog('')
        }
        catch{
            $errorInfo = "error:" + $Error[0]
            $errorInfo
            $Global:logInfo = $Global:logInfo + $errorInfo 
        }
        Finally{
            $Error.clear()
        }
    }
}

